//
//  HTHolderVC.m
//  TruckDriver
//
//  Created by Ting Han on 2017/10/23.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTHolderVC.h"
#import "HTHolderGoodsCell.h"
#import "UIImageView+WebCache.h"
#import "HTHolderVC.h"

@interface HTHolderVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
@property (weak, nonatomic) IBOutlet UILabel *companyLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataSource;

@property (nonatomic,strong) HTHolderModel *holderModel;

@end

@implementation HTHolderVC

- (void)viewDidLoad {
    [super viewDidLoad];
     [self configTableView];
}
- (void)configTableView{
    self.title = @"货主主页";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.width = kHTScreenWidth;
    self.tableView.backgroundColor = kBackgroundColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTHolderGoodsCell" bundle:nil] forCellReuseIdentifier:HolderGoodsCell];
    self.tableView.estimatedRowHeight =0;
    self.tableView.estimatedSectionHeaderHeight =0;
    self.tableView.estimatedSectionFooterHeight =0;
    [self configTableHeader];
    self.tableView.mj_header = self.header;
    self.noDataView = [[HTNoDataView alloc]initWithFrame:self.tableView.bounds];
    self.noDataView.showMessage = @"暂无货源";
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.cornerRadius = 25;
    
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

#pragma mark --- tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    [tableView tableViewDisplayView:self.noDataView ifNecessaryForRowCount:self.dataSource.count];
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HTHolderGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:HolderGoodsCell forIndexPath:indexPath];
    cell.holderListModel = self.dataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}
- (void)loadNewData{
    [self netLoadHolder];
}
- (void)netLoadHolder{
    
    [HTHttpRequestManager requestCargosInforWithDriverid:self.cargosid success:^(id  _Nullable responseObject) {
         [self.tableView.mj_header endRefreshing];
        DBLog(@"获取货主列表：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"货源获取失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:@"货源获取失败"];
            return;
        }
        self.holderModel = [HTHolderModel mj_objectWithKeyValues:responseObject[@"data"]];
        self.dataSource =[HTHolderListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"orderlist"][@"data"]];
        [self.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        [HTShowUtil showMessage:@"货源获取失败"];
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)setHolderModel:(HTHolderModel *)holderModel{
    _holderModel = holderModel;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:holderModel.picurl] placeholderImage:[UIImage imageNamed:@"bg_headsculpture"]options:SDWebImageRefreshCached];
    self.nameLabel.text = holderModel.name;
    self.numberLabel.text = [NSString stringWithFormat:@"发货数%@",holderModel.counts];
    self.addressLabel.text = holderModel.address;
    self.companyLabel.text = holderModel.companyname;
}

@end

@implementation HTHolderModel

@end

